<?php
// +----------------------------------------------------------------------
// | Longbing [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright Chengdu longbing Technology Co., Ltd.
// +----------------------------------------------------------------------
// | Website http://longbing.org/
// +----------------------------------------------------------------------
// | Sales manager: +86-13558882532 / +86-13330887474
// | Technical support: +86-15680635005
// | After-sale service: +86-17361005938
// +----------------------------------------------------------------------

declare(strict_types=1);

namespace app\decorate\info;

use app\card\service\UserService;
use app\decorate\model\Config;
use app\decorate\model\IndexCase;
use app\decorate\model\IndexNews;
use app\decorate\model\IndexSite;
use app\restaurant\info\PermissionRestaurant;
use longbingcore\diy\BaseSubscribe;

/**
 * @author shuixian
 * @DataTime: 2019/12/11 16:23
 * Class Subscribe
 * @package app\ucenter\info
 */
class Subscribe extends BaseSubscribe
{

    private $sort_filed_map = [
        0 => 'sort',
        1 => 'iv',
        2 => 'collect'
    ];


    public function onDiySite($item)
    {
        $data = $item['data'] ;
        $sort_filed = $this->sort_filed_map[$data['sort']] ?? 'id';
        $number = $data['number']['number'];
        $m = new IndexSite();
        $item['dataList'] = $m->where([['uniacid', '=', $this->_uniacid], ['status' , '=', 1]])->order($sort_filed, 'desc')->limit($number)->select();

        return $item;
    }


    public function onDiyCase($item)
    {

        $config_model = new Config();

        $config = $config_model->dataInfo(['uniacid'=>$this->_uniacid]);

        $data = $item['data'] ;

        $sort_filed = $this->sort_filed_map[$data['sort']] ?? 'id';

        $number = $data['number']['number'];

        $m = new IndexCase();

        if($config['case_show']==0){

            $item['dataList'] = $m->where([['uniacid', '=', $this->_uniacid], ['status', '=', 1], ['recommend', '=', 1]])->order($sort_filed, 'desc')->limit($number)->select();
        }else{
            //员工自选
            $number = $config['case_show_num']>$number?$number:$config['case_show_num'];

            $dis=[

                'a.uniacid' => $this->_uniacid,

                'a.status'  => 1,

                'b.user_id' => $item['staff_id']
            ];

//            dump($sort_filed);exit;

            $item['dataList'] = $m->alias('a')
                                ->join('longbing_card_decorate_staff_case b','a.id = b.case_id')
                                ->where($dis)
                                ->field('a.*')
                                ->group('a.id')
                                ->order($sort_filed, 'desc')
                                ->limit($number)
                                ->select()
                                ->toArray();

        }
        return $item;
    }

    public function onDiyStrategy($item)
    {
        $data = $item['data'] ;
        $sort_filed = $this->sort_filed_map[$data['sort']] ?? 'id';
        $number = $data['number']['number'];
        $m = new IndexNews();
        $item['dataList'] = $m->where([['uniacid', '=', $this->_uniacid], ['status', '=', 1]])->order($sort_filed, 'desc')->limit($number)->select();

        return $item;
    }

    /**
     * 相应个人中心对应菜单
     *
     * @return mixed
     * @author shuixian
     * @DataTime: 2019/12/12 11:24
     */
    public function onAddWorkCenterModelMenu(){

        $permission = new PermissionDecorate($this->_uniacid);

        $config_model = new Config();

        $config = $config_model->dataInfo(['uniacid'=>$this->_uniacid]);

        $tmp = $config['case_show']==1?[
            "title" =>  '推荐案例',
            "icon" =>  'iconMyCase',
            "link" =>  '/renovation/pages/case/push',
            "linkType" => 4,
        ]:'';


        if($permission->pAuth()){

            $modelMenu= [
                "title" =>  '装修管理',
                "desc" =>  '',
                "show" => true,
                "row"  => 4,
                "list" =>  [
                    [
                        "title" =>  '我的工地',
                        "icon" =>  'icongongdiweixuanzhong',
                        "link" =>  '/renovation/pages/site/staff?staff_id=' . $this->getUserId(),
                        "linkType" => 4,
                    ],
                    $tmp
                ]
            ];

            return [$modelMenu];
        }

        return [];
    }



    /**
     * 名片展示页获取其他模块数据
     *
     * @param $params
     * @return array
     * @author shuixian
     * @DataTime: 2019/12/24 14:24
     */
    public function onCardInfo($params)
    {

        $site_model = new IndexSite();

        $case_model = new IndexCase();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $data['site'] = $site_model->siteLimit($dis);

        $config_model = new Config();

        $config = $config_model->dataInfo(['uniacid'=>$this->_uniacid]);

        if($config['case_show']==0){

            $dis[] = ['recommend','=',1];

            $data['case'] = $case_model->caseLimit($dis);

        }else{

            $dis=[

                'a.uniacid' => $this->_uniacid,

                'a.status'  => 1,

                'b.user_id' => $params['staff_id']
            ];

            $data['case'] = $case_model->alias('a')
                ->join('longbing_card_decorate_staff_case b','a.id = b.case_id')
                ->where($dis)
                ->field('a.*')
                ->group('a.id')
                ->limit($config['case_show_num'])
                ->select()
                ->toArray();
        }


//        dump($params);exit;

        return ['decorate'=> $data ];
    }


    /**
     * 监听用户中心模块
     *
     * @return array
     * @author shuixian
     * @DataTime: 2019/12/18 14:04
     */
    public function onAddUcenterCompoent(){

        $site  = lbPulgSettingInfo($this->_uniacid,'decorate',['app_title']);

        $app_title = !empty($site['app_title'])?$site['app_title']:'免费设计';


        $last_staff_id = '#last_staff_id#' ;
        $menu = <<<COMPOENT
{
    "title": "我的装修",
    "type": "moduleMenu",
    "icon": "icondecorate",
    "isDelete": true,
    "addNumber": 1,
    "attr": [
        {
            "title": "模板名称",
            "type": "Switch",
            "name": "isShowTitle"
        },
        {
            "title": "选择模板",
            "type": "ChooseModule",
            "name": "module",
            "data": [
                {
                    "title": "一行多列",
                    "name": "module-menu-row",
                    "img": "http://lbqny.migugu.com/admin/diy/module-menu-col.jpg"
                },
                {
                    "title": "一行一列",
                    "name": "module-menu-col",
                    "img": "http://lbqny.migugu.com/admin/diy/module-menu-row.jpg"
                }
            ]
        },
        {
            "title": "一行多少列",
            "type": "InputNumber",
            "name": "row"
        }
    ],
    "data": {
        "isShowTitle": false,
        "module": "module-menu-row",
        "row": {
            "number": 4,
            "min": 2,
            "max": 5,
            "label": "请输入"
        },
        "list": [
            {
                "title": "我的装修",
                "icon": "icongongdiweixuanzhong",
                "link": {
                    "type": 2,
                    "url": "/renovation/pages/site/user"
                }
            },
            {
                "title": "我的收藏",
                "icon": "iconguanzhu",
                "link": {
                    "type": 2,
                    "url": "/renovation/pages/collect"
                }
            },
            {
                "title": "装修报价",
                "icon": "iconzhinengbaojia",
                "link": {
                    "type": 2,
                    "needStaffId": true,
                    "url": "/renovation/pages/offer/offer?from=1&staff_id=$last_staff_id"
                }
            },
            {
                "title": "$app_title",
                "icon": "iconmianfeisheji",
                "link": {
                    "type": 2,
                    "needStaffId": true,
                    "url": "/renovation/pages/design/design?from=1&staff_id=$last_staff_id"
                }
            }
        ]
    }
}
COMPOENT;

        $permission = new PermissionDecorate($this->_uniacid);
        $compoentList = [] ;
        if($permission->pAuth()){

            $compoentList = [

                json_decode($menu, true)
            ] ;
        }

        return $compoentList ;
    }


    /**
     * @param $item
     * @功能说明:监听个人中心装修菜单数据获取
     * @author jingshuixian
     * @DataTime: 2020/1/7 17:02
     */
    public function onDiyModuleMenu($item){

        $last_staff_id =  UserService::getLastStaffId($this->_uniacid,$this->getUserId()) ;

        $site  = lbPulgSettingInfo($this->_uniacid,'decorate',['app_title']);

        $app_title = !empty($site['app_title'])?$site['app_title']:'免费设计';

        $list = [
            [
                "title"=> "我的装修",
                "icon"=> "icongongdiweixuanzhong",
                "link"=> [
                "type"=> 2,
                    "url"=> "/renovation/pages/site/user"
                ]
            ],
            [
                "title"=> "我的收藏",
                "icon"=> "iconguanzhu",
                "link"=> [
                "type"=> 2,
                    "url"=> "/renovation/pages/collect"
                ]
            ],
            [
                "title"=> "装修报价",
                "icon"=> "iconzhinengbaojia",
                "link"=> [
                "type"=> 2,
                    "needStaffId"=> true,
                    "url"=> "/renovation/pages/offer/offer?from=1&staff_id=" . $last_staff_id
                ]
            ],
            [
                "title"=> "$app_title",
                "icon"=> "iconmianfeisheji",
                "link"=> [
                "type"=> 2,
                    "needStaffId"=> true,
                    "url"=> "/renovation/pages/design/design?from=1&staff_id=" . $last_staff_id
                ]
            ]
        ] ;
        $item['data']['list'] = $list ;
        return $item;

    }

}